import useThemeEnjoy from '@enjoy/mobile/utils/useTheme';
const { setTheme } = useThemeEnjoy();

import { defineStore } from 'pinia'

interface ThemeItem {
  c_name: string;
  c_value: string;
}

interface Theme {
  theme: string;
  themeColor: string;
  themeList: ThemeItem[];
  themeBaseUrl: string;
}

export const useThemeStore = defineStore('theme', {
  state: (): Theme => ({
    theme: 'theme-blue',
    themeColor: '#02B7E6',
    themeList: [], // ["theme-blue", "theme-orangeRed"],
    themeBaseUrl: '/static/img/themeBlue/',
  }),
  actions: {
    toggleTheme(e?: string) {
      if (e) {
        this.theme = e;
        this.themeColor = e === 'theme-blue' ? '#02B7E6' : '#FF6430';
        uni.setStorageSync('themeVar', e);
        setTheme(e); // 这里也可以同步设置主题
      } else {
        const themeMap: Record<string, string> = {
          'theme-blue': 'theme-orangeRed',
          'theme-orangeRed': 'theme-blue'
        };

        const newTheme = themeMap[this.theme] || 'theme-blue';
        uni.setStorageSync('themeVar', newTheme);
        this.theme = newTheme;
        this.themeColor = newTheme == 'theme-blue' ? '#02B7E6' : '#FF6430';
        setTheme(newTheme);
      }
      // 转驼峰命名
      const kebabToCamel = (str: string) => str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
      this.themeBaseUrl = `/static/img/${kebabToCamel(this.theme)}/`;
    },
    setThemeList(e: ThemeItem[]) {
      this.themeList = e;
    }
  },
});